var ns;
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        donelist: [],
        form: {
            ID: 0,           
            status: 0, //0=全部,1=异常,2=正常,3=未开始
            StatusName: '全部',
            keywords: '',
        },
        total: {
            ProcessCount: 0,
            CompleteCount: 0
        },
        TabIndex: 1,
        GPSLocation: '',
        statusList: [
            { "ID": "0", "Name": "全部" },
            { "ID": "1", "Name": "异常" },
            { "ID": "2", "Name": "正常" },
            { "ID": "3", "Name": "未开始" },
        ],
    },
    methods: {
        get_data: function() {
            var that = this;
            var options = {};
            options.action = "getequipmenttaskdevicelist";
            options.ID = that.form.ID;
            options.status = that.form.status;
            options.keywords = that.form.keywords;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                    // if (that.form.status == 1) {
                    //     that.list = data.list;
                    // }
                    // if (that.form.status == 2) {
                    //     that.donelist = data.list;                       
                    // }                   
                    that.total = data.total;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_open_scan: function(item) {
            var that = this;
            ns.confirmPer('camera', function() {
                setTimeout(function() {
                    that.do_open_scan_start(item);
                }, 200)
            })
        },
        do_open_scan_start: function(item) {
            var that = this;
            ns.openFrame('scanner_frm', {
                type: 2,
                id: item.ID,
                EncriptID: item.EncriptID
            });
        },
        open_details: function(item) {
            ns.openWin('equipment_details_frm', '巡检设备', {
                id: item.ID
            });
        },
        select_status: function(e) {
            this.form.status = e.target.value;
            this.get_data();
        },
        do_view: function(item) {
            var that = this;
            if (item.RequireScan && item.TaskStatus != 3) {
                that.do_open_scan(item);
                return;
            }
            that.open_details(item);
        },
        do_search: function() {
            this.get_data();
        },
        //id为二维码加密id
        check_scan_result: function(id){
            if(!id){
                ns.openFrame('scanner_frm', {
                    type: 10
                });
            }
            else{
                var that=this;
                var options = {};
                options.action = "qrcoderesult";
                options.ID = that.form.ID;
                options.QRCode = id;
                ns.post(options, function(succeed, data, err) {
                    if (succeed) {
                        // 接口判断，如果成功后跳转详情页
                        ns.openWin('equipment_details_frm', '巡检设备', {
                            id: data.TaskID
                        });
                    } else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
                
            };
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    // 选择项目
                    var pickerStatus = new $.PopPicker({
                        layer: 1,
                        fileID: 'ID',
                        fileName: 'Name',
                    });
                    var statusData = _this.statusList || [];
                    pickerStatus.setData(statusData);
                    var showStatusPickerButton = doc.getElementById('choosestatus');
                    showStatusPickerButton.addEventListener('click', function (event) {
                        pickerStatus.show(function (items) {
                            _this.form.status = items[0].value;
                            _this.form.StatusName = items[0].text;
                            //返回 false 可以阻止选择框的关闭
                            //return false;
                            _this.get_data();
                        });
                    }, false);

                });
            })(mui, document);
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ID = api.pageParam.id || 0;
    ns.init_search();
    toast = new auiToast();
    setTimeout(function() {
        app.get_data();
    }, 500)
    api.addEventListener({
        name: 'devicetaskcomplete'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'scandeviceqrcodedone'
    }, function(ret, err) {
        app.check_scan_result(ret.value.id);
    });
    app.initPicker();
    
}
